package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.mapper.AdminMapper;
import com.fruitshop.model.Admin;
import com.fruitshop.model.Command;
import com.fruitshop.model.UserInfo;
import com.fruitshop.util.ConsoleUtils;
import com.fruitshop.util.DBUtil;
import com.fruitshop.util.PrintUtils;
import com.fruitshop.util.UserInfoContext;

import java.util.List;
import java.util.Scanner;

public class AdminLoginManager extends BaseManager {

    private static final String INPUT_ADMIN_INFO= "1";

    public AdminLoginManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected String processCommand(String command) {
        switch (command) {
            case INPUT_ADMIN_INFO:
                return showLoginInfo();
        }
        return command;
    }
    private String showLoginInfo() {
        while (true) {
            Scanner sc = new Scanner(System.in);
            String account = ConsoleUtils.readString(sc, "请输入账号:");
            String password = ConsoleUtils.readString(sc, "请输入密码:");


            Admin admin = new Admin();
            admin.setAccount(account);
            admin.setPassword(password);
            AdminMapper adminMapper = DBUtil.getMapper(AdminMapper.class);
            Admin byAccount = adminMapper.selectByAccount(admin.getAccount());
            if (byAccount == null||!byAccount.getPassword().equals(admin.getPassword())) {
                PrintUtils.printError("账号或密码错误");
            } else {
                UserInfoContext.setUserInfo(new UserInfo(byAccount, null));
                PrintUtils.printHighLight("登录成功");
                return LOGIN_SUCCESS;
            }
        }
    }
}
